/*
* @file    :record.h
* @author  :LingFengLu
* @date    :2023-07-13
*/

#ifndef _RECORD_H_
#define _RECORD_H_
#include "global.h"
#include "aes_custom.h"
#include "sensor.h"
#include "sensor_config.h"
//Constant definition    宏定义
#define RECORD_SAVE_RETRY          3
#define RAW_CURRENT_SIZE           5 
//////////////////////////////////////////////////////////
#define PAIR_ID_LENGTH             32
#define BOND_ID_LENGTH             6
//Type definition    类型定义

typedef enum
{
	RECORD_PAIR_INFO_FILE_ID         = 0x03,
	RECORD_SENSOR_PARAM_FILE_ID   ,   
	RECORD_HISTORY_HEAD_FILE_ID    ,      
	RECORD_CALIB_PARAM_FILE_ID  ,          
	RECORD_OPERATE_HEAD_FILE_ID  ,        
	RECORD_SETTING_FILE_ID   ,    
	RECORD_COUNT_FILE_ID   , 
}record_file_type ;

typedef enum
{
	HISTORY_TRANSMITTER_STARTUP,
	HISTORY_TRANSMITTER_ERROR,
	HISTORY_BATTERY_LOW,
	HISTORY_BATTERY_EXHAUSTED,
	HISTORY_SENSOR_NEW,
	HISTORY_SENSOR_ERROR,
	HISTORY_SENSOR_EXPIRATION,
	HISTORY_GLUCOSE,
	HISTORY_GLUCOSE_RECOMMEND_CAL,
	HISTORY_GLUCOSE_INVALID,
	HISTORY_HYPO,
	HISTORY_HYPER,
	HISTORY_IMPENDANCE,
	HISTORY_BLOOD_GLUCOSE,
	HISTORY_CALIBRATION,
	HISTORY_HYPO_SETTING,
	HISTORY_HYPER_SETTING,
	HISTORY_PLACEHOLDER = 0x1E,
	HISTORY_INVALID = 0x1F
} record_history_type;


/////////////////////////////////////////////////////
typedef struct
{
	uint8 u8_PairingID[PAIR_ID_LENGTH];
	uint8 u8_BondID[BOND_ID_LENGTH];
	uint8 u8_Key[KEYLEN];
	uint8 u8_PairingIDLength;
	uint8 u8_TransRate;
	uint8 u8_Role;
	uint8 u8_Reserved[3];
} pairing_info;
////////////////////////////////////////////////////////
typedef struct 
{
	sensor_default_param t_SensorParamters;
	uint16 u16_CRC;
}sensor_paramters;
////////////////////////////////////////////////////////
typedef struct
{
	uint8 u8_Glucose;
	uint8 u8_VCounter;
	uint16 u16_CalFactor;
	uint16 u16_IReference;
	uint16 u16_IWorking;
	uint16 u16_IWorkingRaw[RAW_CURRENT_SIZE];
} glu_param;

typedef struct
{
	uint8 u8_Glucose;
	uint8 u8_Frequency;
	sint16 s16_Real;
	sint16 s16_Imag;
	sint16 s16_RealRaw;
	sint16 s16_ImagRaw;
} imp_param;

typedef union
{
	uint8 u8_value;
	uint16 u16_value;
	glu_param t_Glucose;
	imp_param t_Impedance;
} sensor_param;
#pragma pack(2)
typedef struct
{
	uint16 reserve2;
	sint32 s32_DateTime;
	uint16 u16_Index;
	uint8 u8_SensorIndex;
	uint8 u8_Type;
	sensor_param t_Parameter;
} history;
 #pragma pack()
typedef struct
{
	uint16 u16_Index;
	uint8 u8_SensorIndex;
	uint8 u8_Parity;
	sint32 s32_StartTime;
	sint32 s32_RunningTime;
	uint16 u16_AddrIndex;
	uint16 u16_CRC;
} history_header;
/////////////////////////////////////////////////
typedef struct
{
	uint32 u32_CalTime;
	uint16 u16_CalFactor;
	sint16 s16_Offset;
	sint16 s16_RefReal;
	sint16 s16_RefImag;
	sint16 s16_NaBias;
	uint8 u8_SensorIndex;
	uint8 u8_Null;
#if SENSOR_AUTOBASE
	uint32 u32_AutoBaseN;
	float f_AutoBaseB0;
	float f_AutoBaseB;
#endif
} glucose_calib;

/////////////////////////////////////////
typedef enum
{
	OPERATE_RESET_POWER,
	OPERATE_RESET_DOG,
	OPERATE_RESET_ASC,
	OPERATE_SET_TIME,
	OPERATE_GC_OPEN_FAIL,
	OPERATE_INVALID = 0x1F
} operate_type;

typedef struct
{
	sint32 s32_DateTime;
	uint16 u16_Index;
	uint16 u8_OperateType;
}operate_record;

typedef struct
{
	uint16 u16_Index;
	uint16 u16_CRC;
}operate_record_header;
/////////////////////////////
typedef struct
{
	uint32 u32_Flag;
} new_sensor_paramters;

typedef struct
{
	uint16 u8_Hypo;
	uint16 u8_Hyper;
	new_sensor_paramters t_NewSensorParam;
} setting;


//Function declaration    函数申明
uint Record_LoadPairInfo
(
pairing_info * tp_PairInfo,
 uint8 u8_PairInfoNumber
);
uint Record_SavePairInfo
(
pairing_info * tp_PairInfo ,
 uint8 u8_PairInfoNumber
);
uint Record_LoadSensorParam
(
sensor_paramters * tp_SensorParam
);
uint Record_SaveSensorParam
(
sensor_paramters * tp_SensorParam
);
uint Record_LoadHistory
(
uint16 u16_Index,
history * tp_History
);
uint Record_SaveHistory
(
uint16 u16_Index,
history * tp_History
);
uint Record_LoadHistoryHead
(
history_header * tp_HistoryHead
);
uint Record_SaveHistoryHead
(
history_header * tp_HistoryHead
);

uint Record_LoadCaliParam
(
glucose_calib * tp_CalibParam
);
uint Record_SaveCaliParam
(
glucose_calib * tp_CalibParam
);
uint Record_LoadOperate
(
uint16 u16_Index,
operate_record * tp_Operate
);
uint Record_SaveOperate
(
uint16 u16_Index,
operate_record * tp_Operate
);
uint Record_LoadOperateHead
(
operate_record_header * tp_OperateHead
);
uint Record_SaveOperateHead
(
operate_record_header * tp_OperateHead
);
uint Record_LoadSetting
(
setting * tp_Setting
);
uint Record_SaveSetting
(
setting * tp_Setting
);
uint Record_LoadSerialNumber
(
uint8 * u8p_SerialNumber,
uint8 u8_Length   
);



//Golbal variable declaration   全局变量申明










#endif

